//
//  ZTBaseViewController.m
//  AFNetworking
//
//  Created by 武得亮 on 2021/3/12.
//

#import "ZTBaseViewController.h"
#import "ZTIMSetUserDefaultTool.h"
#import "ZTMacros.h"
#import "ZTLibTheme.h"
@interface ZTBaseViewController ()<UIGestureRecognizerDelegate>

@end

@implementation ZTBaseViewController

- (instancetype)init{
    self = [super init];
    if (self) {
        self.modalPresentationStyle = UIModalPresentationFullScreen;
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    //KVO 方式竖屏
//    ZT_AppDelegate.isCustomOrientation = NO;
    if([[UIDevice currentDevice]respondsToSelector:@selector(setOrientation:)]) {
    SEL selector = NSSelectorFromString(@"setOrientation:");
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
        [invocation setSelector:selector];
        [invocation setTarget:[UIDevice currentDevice]];
        int val = UIInterfaceOrientationPortrait;
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSInteger coefficient = [ZTIMSetUserDefaultTool getFontSizeCoefficient];
    self.view.backgroundColor = [UIColor bookRommbgColor];
    float x = 0.075*(coefficient-2)+1;
    self.fontx = x;
    if (IOS11) {
        if (self.navigationController.viewControllers.count > 1) {
            id target = self.navigationController.interactivePopGestureRecognizer.delegate;
            SEL handler = NSSelectorFromString(@"handleNavigationTransition:");

            // 创建全屏滑动手势，调用系统自带滑动手势的target的action方法
            UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:handler];
            
            // 设置手势代理，拦截手势触发
            pan.delegate = self;
            
            // 给导航控制器的view添加全屏滑动手势
            [self.view addGestureRecognizer:pan];
            
            // 禁止使用系统自带的滑动手势
            self.navigationController.interactivePopGestureRecognizer.enabled = NO;
            
            
            /** bundle 引用*/
            NSBundle *associateBunle = [NSBundle bundleWithPath:[[NSBundle bundleForClass:[self class]] pathForResource:@"ZTPrivateLib" ofType:@"bundle"]];
       self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"chat_back"
                                                                                                  inBundle: associateBunle
                                                                                                compatibleWithTraitCollection:nil] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:(UIBarButtonItemStylePlain) target:self action:@selector(backClick)];
        }
    }
}

/**
 全屏滑动返回判断
 
 @param gestureRecognizer 手势
 @return 是否响应
 */
- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer {
    
    if (self.navigationController.isNavigationBarHidden == YES) {
        return NO;
    }
    
    if ([gestureRecognizer translationInView:gestureRecognizer.view].x <= 0) {
        return NO;
    }
    
    return (self.childViewControllers.count != 1);
}

-(BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldReceiveTouch:(UITouch*)touch {
    
    if([touch.view isKindOfClass:[UISlider class]]){
         return NO;
    }
    else
        return YES;
    
}
//修复有水平方向滚动的ScrollView时边缘返回手势失效的问题
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return (otherGestureRecognizer.state == UIGestureRecognizerStateBegan && [otherGestureRecognizer.view isKindOfClass:NSClassFromString(@"UILayoutContainerView")]);
}

- (void)backClick
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


@end
